package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.List;

import com.xcity.game.activity2.circle.CircleShopTemplate;
import com.xcity.game.item.ItemTemplate;

import gnu.trove.map.hash.TIntObjectHashMap;
import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class ActivityCircleShopListResponse extends Response {

	private int curScore;
	private List<CircleShopItem> itemList;
	public static class CircleShopItem{
		public int index;
		public int type;
		public int price;
		public String itemName;
		public String itemDesc;
		public int itemId;
		public String itemIcon;
		public int itemCount;
		public int leftCount;
		public int limitCount;
		public CircleShopItem(CircleShopTemplate temp, String itemName, String itemIcon, String itemDesc, int leftCount, int limitCount){
			this.index = temp.getId();
			this.type = temp.getType();
			this.itemId = temp.getItemId();
			this.itemCount = temp.getItemCount();
			this.price = temp.getPrice();
			this.itemName = itemName;
			this.itemIcon = itemIcon;
			this.itemDesc = itemDesc;
			this.leftCount = leftCount;
			this.limitCount = limitCount;
		}
	}
	public ActivityCircleShopListResponse(int serial, List<CircleShopTemplate> shopItemList, int score, TIntObjectHashMap<Integer> id2count, boolean isnew) {
		super(serial);
		this.curScore = score;
		this.itemList = new ArrayList<CircleShopItem>();
		TemplateService ts = App.getApp().getServiceManager().get(TemplateService.class);
//		shopItemList = ts.getTemplatesList(CircleShopTemplate.class);
		int size = shopItemList.size();
		for(int i = 0; i < size; i++){
			CircleShopTemplate shopItemTemp = shopItemList.get(i);
			if(isnew == (shopItemTemp.getShopType() == 0)){
				continue;
			}
			int limitCount = shopItemTemp.getLimitCount();
			int leftCount = -1;
			if(limitCount > 0){
				if(id2count.containsKey(shopItemTemp.getId())){
					leftCount = limitCount - id2count.get(shopItemTemp.getId());
				}else{
					leftCount = limitCount;
				}
			}
			
			CircleShopItem shopItem = null;
			String name = null;
			String icon = null;
			String desc = null;
			switch (shopItemTemp.getType()) {
			case 1://金钱
				name = "现金";
				icon = shopItemTemp.getItemIcon();
				desc = "";//TODO 金钱描述
				shopItem = new CircleShopItem(shopItemTemp, name, icon, desc, leftCount, limitCount);
				itemList.add(shopItem);
				break;
			case 2://物品
				ItemTemplate itemTemp = ts.get(ItemTemplate.class, shopItemTemp.getItemId());
				if(itemTemp != null){
					name = itemTemp.getName();
					icon = itemTemp.getIcon();
					desc = itemTemp.getDesc();
					shopItem = new CircleShopItem(shopItemTemp, name, icon, desc, leftCount, limitCount);
					itemList.add(shopItem);
				}
				break;
			default:
				break;
			}
		}
	}
	
	public List<CircleShopItem> getItemList() {
		return itemList;
	}
	public void setItemList(List<CircleShopItem> itemList) {
		this.itemList = itemList;
	}
	public int getCurScore() {
		return curScore;
	}
	public void setCurScore(int curScore) {
		this.curScore = curScore;
	}
}
